#include "Cats/CatStates/CatState.h"
#include "Cats/CatStates/CatState_Base_Flank.h"
#include "Cats/BaseCat.h"
#include "Cats/CatSpriteLoader.h"
#include "Character/Character.h"

CatState_Base_Flank::CatState_Base_Flank(BaseCat* pCatRef):CatState(pCatRef)
{
	_exitState = false;
}

CatState_Base_Flank::~CatState_Base_Flank()
{

}

void CatState_Base_Flank::runOnEnter()
{
	//play the running animation if the previous state is not chasing
	if (_pCatRef->getPreviousCatState() != CAT_CHASE)
	{
		_pCatRef->stopAllActions();
		_pCatRef->runAction(CatSpriteLoader::getCatAnimation(_pCatRef->getCatType(), CAT_ANIM_WALK));
	}
}

void CatState_Base_Flank::update(float timeDiff)
{
	// Run straight out of screen for a certain distance and run back
	//the cat is not far enough outside of screen yet
	if(_pCatRef->getPositionX() >= BASE_FLANK_DIST)
	{
		_pCatRef->move(timeDiff);

		//check collision with character
		if ( _exitState || _pCatRef->getRect().intersectsRect(_pCharRef->getRect()) )
		{
			if (!_exitState) //first time hitting the character)
			{
				//exit this state
				_exitState = true;
				//reduce the character's health
				_pCharRef->reduceHealth(_pCatRef->getBaseAttackDamage());
			}
			//kill this cat
			_pCatRef->setNextCatState(CAT_DYING);
		}
	}
	else
	{
		// Run back to screen and Attack character
		_pCatRef->setNextCatState(CAT_ATTACK);
	}
}

void CatState_Base_Flank::onExit()
{
	CatState::onExit();
	// Flip cat sprite
	_pCatRef->flipCatSprite();
}

void CatState_Base_Flank::onResume()
{
	CatState::onResume();

	runOnEnter();
}
